import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["actionRender", "creatorButtonProps", "label", "alwaysShowItemLabel", "tooltip", "creatorRecord", "itemRender", "rules", "itemContainerRender", "fieldExtraRender", "copyIconProps", "children", "deleteIconProps", "actionRef", "style", "prefixCls", "actionGuard", "min", "max", "colProps", "rowProps"];
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { CopyOutlined, DeleteOutlined } from '@ant-design/icons';
import { ProFormContext } from '@ant-design/pro-utils';
import { ConfigProvider, Form } from 'antd-v4';
import { noteOnce } from "rc-util/es/warning";
import React, { useContext, useEffect, useImperativeHandle, useMemo, useRef } from 'react';
import { useGridHelpers } from '../../helpers';
import './index.less';
import { ProFormListContainer } from './ListContainer';
var FormListContext = /*#__PURE__*/React.createContext({});

function ProFormList(_ref) {
  var actionRender = _ref.actionRender,
      creatorButtonProps = _ref.creatorButtonProps,
      label = _ref.label,
      alwaysShowItemLabel = _ref.alwaysShowItemLabel,
      tooltip = _ref.tooltip,
      creatorRecord = _ref.creatorRecord,
      itemRender = _ref.itemRender,
      rules = _ref.rules,
      itemContainerRender = _ref.itemContainerRender,
      fieldExtraRender = _ref.fieldExtraRender,
      _ref$copyIconProps = _ref.copyIconProps,
      copyIconProps = _ref$copyIconProps === void 0 ? {
    Icon: CopyOutlined,
    tooltipText: '复制此行'
  } : _ref$copyIconProps,
      _children = _ref.children,
      _ref$deleteIconProps = _ref.deleteIconProps,
      deleteIconProps = _ref$deleteIconProps === void 0 ? {
    Icon: DeleteOutlined,
    tooltipText: '删除此行'
  } : _ref$deleteIconProps,
      actionRef = _ref.actionRef,
      style = _ref.style,
      prefixCls = _ref.prefixCls,
      actionGuard = _ref.actionGuard,
      min = _ref.min,
      max = _ref.max,
      colProps = _ref.colProps,
      rowProps = _ref.rowProps,
      rest = _objectWithoutProperties(_ref, _excluded);

  var actionRefs = useRef();
  var context = useContext(ConfigProvider.ConfigContext);
  var listContext = useContext(FormListContext);
  var baseClassName = context.getPrefixCls('pro-form-list');

  var _useGridHelpers = useGridHelpers({
    colProps: colProps,
    rowProps: rowProps
  }),
      ColWrapper = _useGridHelpers.ColWrapper,
      RowWrapper = _useGridHelpers.RowWrapper; // 处理 list 的嵌套


  var name = useMemo(function () {
    if (listContext.name === undefined) {
      return [rest.name].flat(1);
    }

    return [listContext.name, rest.name].flat(1);
  }, [listContext.name, rest.name]);
  var proFormContext = useContext(ProFormContext); // eslint-disable-next-line react-hooks/exhaustive-deps

  useImperativeHandle(actionRef, function () {
    return _objectSpread(_objectSpread({}, actionRefs.current), {}, {
      get: function get(index) {
        return proFormContext.formRef.current.getFieldValue([].concat(_toConsumableArray(name), [index]));
      },
      getList: function getList() {
        return proFormContext.formRef.current.getFieldValue(_toConsumableArray(name));
      }
    });
  }, [name, proFormContext.formRef]);
  useEffect(function () {
    noteOnce(!!proFormContext.formRef, "ProFormList \u5FC5\u987B\u8981\u653E\u5230 ProForm \u4E2D,\u5426\u5219\u4F1A\u9020\u6210\u884C\u4E3A\u5F02\u5E38\u3002");
    noteOnce(!!proFormContext.formRef, "Proformlist must be placed in ProForm, otherwise it will cause abnormal behavior.");
  }, [proFormContext.formRef]);
  if (!proFormContext.formRef) return null;
  return _jsx(ColWrapper, {
    children: _jsx("div", {
      className: baseClassName,
      style: style,
      children: _jsx(Form.Item, _objectSpread(_objectSpread({
        label: label,
        prefixCls: prefixCls,
        tooltip: tooltip,
        style: style
      }, rest), {}, {
        name: undefined,
        rules: undefined,
        children: _jsx(Form.List, _objectSpread(_objectSpread({
          rules: rules
        }, rest), {}, {
          name: name,
          children: function children(fields, action, meta) {
            // 将 action 暴露给外部
            actionRefs.current = action;
            return _jsxs(RowWrapper, {
              children: [_jsx(ProFormListContainer, {
                name: name,
                originName: rest.name,
                copyIconProps: copyIconProps,
                deleteIconProps: deleteIconProps,
                formInstance: proFormContext.formRef.current,
                prefixCls: baseClassName,
                meta: meta,
                fields: fields,
                itemContainerRender: itemContainerRender,
                itemRender: itemRender,
                fieldExtraRender: fieldExtraRender,
                creatorButtonProps: creatorButtonProps,
                creatorRecord: creatorRecord,
                actionRender: actionRender,
                action: action,
                actionGuard: actionGuard,
                alwaysShowItemLabel: alwaysShowItemLabel,
                min: min,
                max: max,
                count: fields.length,
                children: _children
              }), _jsx(Form.ErrorList, {
                errors: meta.errors
              })]
            });
          }
        }))
      }))
    })
  });
}

export { FormListContext, ProFormList };